<template>
    <div>
        <table-search
            ref="table-search"
            :selectHead="selectHead"
            @searchMethods="searchMethods"
            :teacherOptions="teacherOptions"
            :nameOptions="nameOptions"></table-search>
        <el-table
            ref="table"
            :data="tableData"
            border
            @sort-change="tableSort"
        >
            <el-table-column
                label="ID"
                min-width="50"
                align="left">
                <template slot-scope="scope">
                    {{(scope.$index + 1) + (current_page - 1) * per_page}}
                </template>
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="申请人"
                min-width="100"
                show-overflow-tooltip
                prop="name">
            </el-table-column>
            <el-table-column
                label="完成案件数(件)"
                prop="agent_finals_count"
                align="left"
                sortable="custom"
                min-width="150">
            </el-table-column>
            <el-table-column
                label="导师"
                sortable="custom"
                align="left"
                min-width="100"
                show-overflow-tooltip
                prop="teacher"
            >
            </el-table-column>
            <el-table-column
                label="来源"
                sortable="custom"
                align="left"
                min-width="110"
                prop="source"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    {{scope.row.source}}
                </template>
            </el-table-column>
            <el-table-column
                label="导师意见"
                sortable
                align="left"
                min-width="120"
                show-overflow-tooltip
            >
                <template>
                    <span class="finish">通过</span>
                </template>
            </el-table-column>
            <el-table-column
                label="申请时间"
                sortable="custom"
                align="left"
                prop="apply_time"
                show-overflow-tooltip
                min-width="175">
                <!-- <template slot-scope="scope">
                    {{scope.row.user_record.findIndex(v => v.type == 2) > -1 ? scope.row.user_record[scope.row.user_record.findIndex(v => v.type == 2)].created_at : (scope.row.user_record.findIndex(v => v.type == 3) > -1 ? scope.row.user_record[scope.row.user_record.findIndex(v => v.type == 3)].created_at : scope.row.user_record[0].created_at)}}
                </template> -->
            </el-table-column>
            <el-table-column
                label="注册时间"
                align="left"
                prop="register_time"
                sortable="custom"
                show-overflow-tooltip
                min-width="175">
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                min-width="140">
                <template slot-scope="scope">
                    <el-button type="primary" :disabled="!scope.row.can_operate" size="small" @click="applyOperate(scope.row, 'yes')">通过</el-button>
                    <el-button type="danger" :disabled="!scope.row.can_operate" size="small" @click="applyOperate(scope.row, 'no')">拒绝</el-button>
                    <!-- <el-button type="primary" :disabled="scope.row.user_record.findIndex(v => v.type == 3) == -1 ||　scope.row.user_record.findIndex(v => v.type == 5) > -1 || scope.row.user_record.findIndex(v => v.type == 6) > -1" size="small" @click="applyOperate(scope.row, 'yes')">通过</el-button> -->
                    <!-- <el-button type="danger" :disabled="scope.row.user_record.findIndex(v => v.type == 3) == -1 ||　scope.row.user_record.findIndex(v => v.type == 5) > -1 || scope.row.user_record.findIndex(v => v.type == 6) > -1" size="small" @click="applyOperate(scope.row, 'no')">拒绝</el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <pagination @paginationChange="pageChange" :total="total" v-if="table_show"></pagination>
    </div>
</template>

<script>
import pagination from '~/components/pagination'
import { getMyInfo } from "~/api/user";
import tableSearch from '~/components/tableSearch'
export default {
    components: {
        pagination,
        tableSearch
    },

    data() {
        return {
            table_show: true,
            selectHead: [
                {
                    label: '申请人',
                    props: 'name'
                },
                {
                    label: '完成案件数(件)',
                    props: 'agent_finals_count'
                },
                {
                    label: '来源',
                    props: 'source'
                },
                {
                    label: '导师',
                    props: 'teacher'
                },
                {
                    label: '申请时间',
                    props: 'apply_time'
                },
                {
                    label: '注册时间',
                    props: 'created_at'
                }
            ],
            tableData: [],
            current_page: 1,
            per_page: 25,
            total: 0,
            sort_params: {},
            search_params: {},
            nameOptions: [],
            teacherOptions: []
        }
    },

    created() {
        this.getTableData()
    },

    methods: {
        // 表头排序
        tableSort(column) {
            let params = {
                sort_str: column.prop,
            }
            if (column.order == 'ascending') {
                params.sort_order = 'asc'
            } else if (column.order == 'descending') {
                params.sort_order = 'desc'
            } else {
                params = {}
            }
            this.sort_params = params
            this.getTableData()
        },
        searchMethods(params, no_search) {
            this.sort_params = {}
            this.$refs['table'].clearSort()
            console.log(params)
            this.search_params = params
            if (!no_search) {
                this.per_page = 25
                this.current_page = 1
                this.table_show = false
                this.$nextTick(() => {
                    this.table_show = true
                })
                this.getTableData()
            }
        },
        async getMyinfo () {
            let myInfo = await getMyInfo(this.$axios);
            if (myInfo.data.status == 'success') {
                this.$store.commit("setRoleInfo",myInfo.data.data)
                this.$store.commit("setUser",myInfo.data.data);
            }
        },
        applyOperate(data, type) {
            let msg = `确认${type == 'yes' ? '通过' : '拒绝'}<span class="finish">${data.name}</span>的出师申请吗？`
            this.$confirm(msg, '提示', {
                type: 'warning',
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                center: true,
                dangerouslyUseHTMLString: true
            }).then(() => {
                let params = {
                    agent_id: data.id,
                    pass: type
                }
                this.$axios.post('/api/agent/process_graduation_audit', params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: `${type == 'yes' ? '通过' : '拒绝'}出师申请成功`
                        })
                        this.current_page = 1
                        this.getMyinfo()
                        this.getTableData()
                    } else {
                        this.$message({
                            type: 'error',
                            message:　res.data.message
                        })
                    }
                })
            }).catch(() => {})
        },
        pageChange(data) {
            this.per_page = data.perPage
            this.current_page = data.page
            this.getTableData()
        },
        getTableData() {
            let params = {
                page: this.current_page,
                perPage: this.per_page
            }
            params = {...params, ...this.sort_params, ...this.search_params}
            this.$axios.get('/api/agent/graduation_list', {
                params: params
            }).then(res => {
                if (res.data.code == 200) {
                    this.tableData = res.data.data
                    this.teacherOptions = res.data.allTeacher
                    this.nameOptions = res.data.allName
                    if (this.tableData.length > 0) {
                        this.tableData.forEach(v => {
                            v.can_operate = true
                            if (v.userRecord[0].type == 5 || v.userRecord[0].type == 6) {
                                v.can_operate = false
                            }
                        });
                    }
                    this.total = res.data.page.total
                } else {
                    this.$message({
                        type: 'error',
                        message: res.data.message
                    })
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>

</style>
